using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class HelpContextEditor : UserControl, ISavableControl
    {
        private bool _hasChanges = false;
        private HelpContext _helpContext = null;

        public HelpContext CurrentHelpContext
        {
            get { return _helpContext; }
            set { _helpContext = value; }
        }

        public HelpContextEditor()
        {
            InitializeComponent();
            this.Leave += new EventHandler(CheckUnsavedData);
        }

        public void LoadHelpContext()
        {
            if (this.CurrentHelpContext != null)
            {
                textBoxName.Text = CurrentHelpContext.name;
                textBoxId.Text = CurrentHelpContext.id;
                textBoxCreatedDate.Text = CurrentHelpContext.createdDate;
                textBoxChangedDate.Text = CurrentHelpContext.changedDate;
                textBoxAuthor.Text = CurrentHelpContext.author;
                LoadVersions();
            }
        }

        public bool HasUnsavedData
        {
            get
            {
                bool result = false;

                if (_hasChanges) return _hasChanges;

                if (string.Compare(textBoxName.Text, CurrentHelpContext.name, false) != 0)
                    result = true;

                if (string.Compare(textBoxId.Text, CurrentHelpContext.id, false) != 0)
                    result = true;

                if (string.Compare(textBoxAuthor.Text, CurrentHelpContext.author, false) != 0)
                    result = true;

                if (string.Compare(ReadVersionString(), CurrentHelpContext.version, false) != 0)
                    result = true;

                return result;
            }
        }

        public void SaveData()
        {
            if (HasUnsavedData)
            {
                if (string.IsNullOrEmpty(textBoxId.Text))
                {
                    MessageBox.Show("Help Item must have Id, cannot save", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                CurrentHelpContext.name = textBoxName.Text;
                CurrentHelpContext.id = textBoxId.Text;
                CurrentHelpContext.changedDate = DateTime.Now.ToString(Utils.DEFAULT_DATETIME_FORMAT_STRNG);
                CurrentHelpContext.author = textBoxAuthor.Text;
                CurrentHelpContext.version = ReadVersionString();

                //manifest entry
                CurrentHelpContext.ManifestEntry.id = CurrentHelpContext.id;

                HelpContext tmp = HCProjectManager.CurrentProject.HelpContext.Find(delegate(HelpContext con) { return string.Compare(con.id, CurrentHelpContext.id, true) == 0; });

                if (tmp == null)
                    HCProjectManager.CurrentProject.HelpContext.Add(CurrentHelpContext);
                else
                    tmp = CurrentHelpContext;
            }
        }

        public void CheckUnsavedData(object sender, EventArgs e)
        {
            if (this.HasUnsavedData)
            {
                this.SaveData();
                Global.HAS_CHANGES_IN_MEMORY_NOT_SAVED_TO_DISK = true;
            }
        }

        private void btnParentHelpItem_Click(object sender, EventArgs e)
        {
            using (ParentHelpItemChooser chooser = new ParentHelpItemChooser())
            {
                chooser.LoadData(CurrentHelpContext.ManifestEntry);
                DialogResult dResult = chooser.ShowDialog(this);
                if (dResult == DialogResult.OK)
                {
                    if (CurrentHelpContext.ManifestEntry == null)
                        CurrentHelpContext.ManifestEntry = new HelpItemManifestEntry();

                    _hasChanges = chooser.HasChanges;

                    CurrentHelpContext.ManifestEntry.Parents = chooser.ParentEntries;
                }
            }
        }

        private void buttonRelatedItem_Click(object sender, EventArgs e)
        {
            using (RelatedItemChooser chooser = new RelatedItemChooser())
            {
                chooser.LoadData(CurrentHelpContext.ManifestEntry);
                DialogResult dResult = chooser.ShowDialog(this);
                if (dResult == DialogResult.OK)
                {
                    CurrentHelpContext.ManifestEntry.relatedItemsPoitingToMe = chooser.RelatedItems;
                    _hasChanges = chooser.HasChanges;
                }
            }
        }

        private void DigitOnly(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Digit only", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                e.Handled = true;
            }
        }

        private string ReadVersionString()
        {
            return string.Concat(textBoxVersion1.Text, ".", textBoxVersion2.Text, ".", textBoxVersion3.Text, ".", textBoxVersion4.Text);
        }

        private void LoadVersions()
        {
            if (string.IsNullOrEmpty(CurrentHelpContext.version))
                CurrentHelpContext.version = HCProjectManager.CurrentProject.ProjectManifest.version;

            string verionsString = CurrentHelpContext.version;
            
            string[] strs = System.Text.RegularExpressions.Regex.Split(verionsString, "\\.");
            if (strs.Length != 4)
                throw new Exception(string.Concat("Version number must have four parts, error version string: ", verionsString));
            textBoxVersion1.Text = strs[0];
            textBoxVersion2.Text = strs[1];
            textBoxVersion3.Text = strs[2];
            textBoxVersion4.Text = strs[3];
        }
    }
}
